def fetchSQLData(shost,suser,spasswd,sdb,stabl=None,action="s",cn="MySQL"):
    """SQL Data Recollection function
       shost = HOST
       suser = USER
       spasswd = PASSWORD
       sdb = DATABASE
       stabl = TABLE (optional)
       action = s Show   
                v Verbose (return and show)             
                r Return 
       cn = CONECTION TYPE (default MySQL)         
    """
    # RecordSet Constants
    IDEN = 0
    FORM = 2
    LEN1 = 4
    LEN2 = 5
    DESC = 7
    ISPK = 6
    ISKE = 6
    ANUL = 3
    INDE = 1    
    # Connection Types
    if cn=="MySQL":
        import MySQLdb
        try:
            db = MySQLdb.connect(host=shost, user=suser, passwd=spasswd,db="information_schema")
        except MySQLdb.Error, e:
            print "A Mysql Error has ocurred. CodMACs is closing"            
            print "Information:"            
            print 
            print "MySQL Error %d: %s" % (e.args[0], e.args[1])
            raise SystemExit                      
    elif cn=="MSSQL": # Reserved for Microsoft MySQL
        print cn + " Not implemented"
        raise SystemExit    
    elif cn=="ORACLE": # Reserved for Oracle 
        print cn + " Not implemented"
        raise SystemExit
    elif cn=="POSTGRE": # Reserved for PostgreSQL
        print cn + " Not implemented"
        raise SystemExit
    elif cn=="FIREBIRD": # Reserved for Firebird SQL
        print cn + " Not implemented"
        raise SystemExit                
    else:
        print cn + " Not supported"          
        raise SystemExit
    # SQL Stuff
    cursor = db.cursor()                    
    if stabl==None or stabl=="": # Return avaible tables
        sql="SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA='" + sdb + "'"
        cursor.execute(sql)   
        result = cursor.fetchall()   
        print "List of Tables in DataBase: " + sdb
        print "--------------"
        for rs in result:
            print "[" + rs[IDEN] + "]"
        print "--------------"
    else:      
        # Read Gathering Query from special file
        sql = fileSQL(cn).replace("$sdb",sdb).replace("$stabl",stabl)
        
        print sql
            
        #sql = "SELECT COLUMN_NAME, ORDINAL_POSITION, DATA_TYPE, IS_NULLABLE," 
        #sql += " IFNULL(CHARACTER_MAXIMUM_LENGTH,-1), IFNULL(NUMERIC_PRECISION,-1)"
        #sql += " ,COLUMN_KEY,"
        #sql += " COLUMN_COMMENT"
        #sql += " FROM `COLUMNS`"
        #sql += " WHERE TABLE_SCHEMA='" + sdb + "'" 
        #sql += " AND TABLE_NAME='" + stabl + "'"
        #sql += " ORDER BY ORDINAL_POSITION"
        cursor.execute(sql)   
        result = cursor.fetchall()   
        # Verbose and/or Return
        if action=="s" or action=="v" or action=="2v":
            print "[" + stabl + "]"
            print "--------------"        
        if action=="r" or action=="v" or action=="2" or action=="2v":
            ret = [] # Returned by function     
        for rs in result:
            if action=="r" or action=="v" or action=="2" or action=="2v":
                buf = (str(rs[IDEN]),str(rs[FORM]),toggleNull(rs[LEN1],rs[LEN2]),str(rs[DESC]),ifPK(rs[ISPK]),ifKE(rs[ISKE]),ifNULL(rs[ANUL]),rs[INDE])                                
                ret.append(buf)                
            if action=="s" or action=="v" or action=="2v":
                print str(rs[INDE]) + "-" + str(rs[IDEN]) + " " + (str(rs[FORM]) + "(" + str(toggleNull(rs[LEN1],rs[LEN2])) + ")").ljust(15) + str(rs[DESC])                                             
        if action=="s" or action=="v" or action=="2v":    
            print "--------------"        
        if action=="r" or action=="v" or action=="2" or action=="2v":
            return ret

def fileSQL(f): #Opens file with SQL structure Query
    fs = open(f + ".sql","r")  
    sql = ""
    for lin in fs:  
        sql += str(lin)    
    fs.close()  
    return sql

def toggleNull(v1,v2): #Removes MySQL Nulls
    """Switches between two values, one (-1) None """
    if v1 == -1:
        return v2
    else:
        return v1          
        
def ifPK(v): #Is Primary Key       
    if v.upper() == "PRI":
        return True
    else:
        return False

def ifKE(v): #Is Key  
    if v.upper() == "MUL":
        return True
    else:
        return False
        
def ifNULL(v): #Allow Nulls
    if v.upper() == "YES" or v.upper() == "SI":
        return True
    else:
        return False                                       
        
        
        
